<template>
  <Cesium v-if="sceneStore.sceneDetail" />
</template>

<script setup lang="ts">
import { useRoute, useRouter } from 'vue-router';
import Cesium from '@/views/scene/detail/components/cesium.vue';
import { useSceneStore } from '@/store/scene';
import { useRequest } from '@jl/common/composable';
import { getSceneDetailNoAuth, shareVerifyTicket } from '@/api/scene';
import { clearModelReadyMap, clearLayerMap, clearLayerClickCallbackMap } from '@/service';
import { layerRequestInsMap } from '@/service/effect';
import { SCENE_LOGICMARK_START } from '@/views/enum/scene';

defineOptions({
  name: 'ScenePreviewValidate',
});

const sceneStore = useSceneStore();
const route = useRoute();
const router = useRouter();
const { run: basicDetailRun, data: basicDetailData } = useRequest<any, any>(
  getSceneDetailNoAuth,
  null
);

onMounted(async () => {
  const shareCode = route.params.shareCode as any;
  if (!shareCode) return;
  const sceneId = await shareVerifyTicket({
    data: { recordAccess: 1, shareCode: shareCode },
  });
  await basicDetailRun({
    data: {
      recordAccess: 1,
      shareCode: shareCode,
      resourceCode: sceneId.resourceCode,
    },
  });
  if (basicDetailData.value.logicMark !== SCENE_LOGICMARK_START) {
    router.push('/share/noauth');
    return;
  }
  sceneStore.sceneDetail = basicDetailData.value;
});

onBeforeUnmount(() => {
  sceneStore.layerList = [];
  sceneStore.onlineMapSels = [];
  sceneStore.isCimupComplete = false;
  sceneStore.leftPanelActive = '';
  sceneStore.preview = false;
  sceneStore.leftPanelVisible = false;
  sceneStore.mapPanelVisible = false;
  sceneStore.sceneDetail = null;
  sceneStore.mapCameraHeight = 100000;
  sceneStore.versionList = [];
  clearModelReadyMap();
  clearLayerMap();
  clearLayerClickCallbackMap();
  sceneStore.selLayer = null;
  sceneStore.specialEffectVisible = false;
  [...layerRequestInsMap.keys()].forEach((layerName) => {
    const ins: any = layerRequestInsMap.get(layerName);
    ins.destroy();
    layerRequestInsMap.delete(layerName);
  });
});
</script>

<style lang="less" scoped></style>
